/**
 * 
 */

package com.wutianyi.activemq.example;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSubscriber;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * @author wutianyi
 * 
 */
public class TopicTest {

	public static void main(String[] args) throws JMSException {

		ActiveMQConnectionFactory connectionFactory =
				new ActiveMQConnectionFactory("tcp://localhost:61616");
		Connection connection = connectionFactory.createConnection();
//		connection.setClientID("wutianyi");
		connection.start();
		Session session =connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination topic = session.createTopic("Topic.TEST");
		MessageProducer producer = session.createProducer(topic);
		
		producer.send(session.createTextMessage("hello"));
		
		Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Topic t = s.createTopic("Topic.TEST");
		
		TopicSubscriber consumer = s.createDurableSubscriber(t, "wutianyi");
		TextMessage textMessage = (TextMessage)consumer.receive(1000);
		System.out.println(textMessage.getText());
		
		session.close();
		connection.stop();
		connection.close();
	}
}
